package com.mas.admin.biz.controller.admin.users;

import com.mas.admin.biz.convert.users.UserConvert;
import com.mas.admin.biz.dal.dataobject.users.AdminUserDO;
import com.mas.admin.biz.model.vo.users.UserCreateReqVO;
import com.mas.admin.biz.model.vo.users.UserPageItemRespVO;
import com.mas.admin.biz.model.vo.users.UserPageReqVO;
import com.mas.admin.biz.model.vo.users.UserRespVO;
import com.mas.admin.biz.model.vo.users.UserSimpleRespVO;
import com.mas.admin.biz.model.vo.users.UserUpdatePasswordReqVO;
import com.mas.admin.biz.model.vo.users.UserUpdateReqVO;
import com.mas.admin.biz.model.vo.users.UserUpdateStatusReqVO;
import com.mas.admin.biz.service.users.AdminUserService;
import com.mas.framework.common.core.enums.CommonStatusEnum;
import com.mas.framework.common.pojo.CommonResult;
import com.mas.framework.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.mas.framework.common.pojo.CommonResult.success;

/**
 * @author godyao
 * @description 管理后台 - 用户
 * @date 2023年01月19日17:34
 */
@Api(tags = "管理后台 - 用户")
@RestController
@RequestMapping("/system/user")
@Validated
public class UserController {
    @Resource
    private AdminUserService userService;

    @ApiOperation(value = "新增用户")
    @PostMapping("/create")
    public CommonResult<Long> createUser(@Valid @RequestBody UserCreateReqVO reqVO) {
        Long id = userService.createUser(reqVO);
        return success(id);
    }

    @PreAuthorize("@ss.hasAnyRoles('role1')")
    @ApiOperation(value = "根据用户ID查找用户")
    @GetMapping("/get")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<UserRespVO> getInfo(@RequestParam("id") Long id) {
        final AdminUserDO user = userService.getUser(id);
        return success(UserConvert.INSTANCE.convert(user));
    }

    @PutMapping("update")
    @ApiOperation("修改用户")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody UserUpdateReqVO reqVO) {
        userService.updateUser(reqVO);
        return success(true);
    }

    @DeleteMapping("delete")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @PutMapping("update-password")
    @ApiOperation("修改用户密码")
    public CommonResult<Boolean> updateUserPassword(@Valid @RequestBody UserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(reqVO.getId(), reqVO.getPassword());
        return success(true);
    }

    @PutMapping("/update-status")
    @ApiOperation("修改用户状态")
    public CommonResult<Boolean> updateUserStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        userService.updateUserStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @GetMapping("getUserPage")
    @ApiOperation(value = "分页获取用户数据")
    public CommonResult<PageResult<UserPageItemRespVO>> getUserPage(@Valid UserPageReqVO reqVO) {
        // 获得用户分页列表
        PageResult<UserPageItemRespVO> pageResult = userService.getUserPage(reqVO);
        return success(pageResult);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取用户精简信息列表", notes = "只包含被开启的用户，主要用于前端的下拉选项")
    public CommonResult<List<UserSimpleRespVO>> getSimpleUsers() {
        // 获用户列表，只要开启状态的
        List<AdminUserDO> list = userService.getUsersByStatus(CommonStatusEnum.ENABLE.getStatus());
        // 排序后，返回给前端
        return success(UserConvert.INSTANCE.convertList(list));
    }

}
